
package com.wyzz.hr.bo.impl;

import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IDeptHistBo;
import com.wyzz.hr.dao.IDeptHistDao;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Depthist;


public class DeptHistBoImpl implements IDeptHistBo {

	public DeptHistBoImpl() {
	}

	public IDeptHistDao getDeptHistDao() {
		return deptHistDao;
	}

	public void setDeptHistDao(IDeptHistDao deptHistDao) {
		this.deptHistDao = deptHistDao;
	}

	public List getDhsBeforeQueryDate(Date queryDate) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Depthist.class);
		dc.add(Restrictions.eq(Depthist.PROP_DHI_DEPT_STATUS, Integer
				.valueOf(1)));
		dc.add(Restrictions.le(Depthist.PROP_DHI_VALID_FROM, queryDate));
		dc.add(Restrictions.or(Restrictions.ge(Depthist.PROP_DHI_VALID_TO,
				queryDate), Restrictions.isNull(Depthist.PROP_DHI_VALID_TO)));
		return deptHistDao.findByCriteria(dc);
	}

	public Depthist getLatestHistOfDept(Department dept) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Depthist.class);
		dc.add(Restrictions.eq(Depthist.PROP_DHI_DEPT_NO, dept));
		dc.add(Restrictions.isNull(Depthist.PROP_DHI_VALID_TO));
		List deptHistList = deptHistDao.findByCriteria(dc);
		return deptHistList.size() <= 0 ? null : (Depthist) deptHistList.get(0);
	}

	public Map getLatestHistOfDept(List deptList) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Depthist.class);
		dc.add(Restrictions.in(Depthist.PROP_DHI_DEPT_NO, deptList));
		dc.add(Restrictions.isNull(Depthist.PROP_DHI_VALID_TO));
		List deptHistList = deptHistDao.findByCriteria(dc);
		Map deptHistMap = new HashMap();
		Depthist hist;
		for (Iterator i$ = deptHistList.iterator(); i$.hasNext(); deptHistMap
				.put(hist.getDhiDeptNo().getId(), hist))
			hist = (Depthist) i$.next();

		return deptHistMap;
	}

	public List getAllDhsOfDept(String deptId) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Depthist.class);
		dc.setFetchMode(Depthist.PROP_DHI_DEPT_NAME, FetchMode.JOIN);
		dc.setFetchMode(Depthist.PROP_DHI_DEPT_SUP_ID, FetchMode.JOIN);
		dc.add(Restrictions.eq((new StringBuilder()).append(
				Depthist.PROP_DHI_DEPT_NO).append(".id").toString(), deptId));
		dc.addOrder(Order.desc(Depthist.PROP_DHI_VALID_FROM));
		List deptHistList = deptHistDao.findByCriteria(dc);
		return deptHistList;
	}

	private IDeptHistDao deptHistDao;
}
